CODE 85. Rotate Image

版权声明:本文为博主原创文章,转载请注明出处,谢谢!

版权声明:本文为博主原创文章,转载请注明出处:http://blog.jerkybible.com/2013/10/19/2013-10-19-CODE 85 Rotate Image/

访问原文「CODE 85. Rotate Image

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Follow up:
Could you do this in-place?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void rotate(int[][] matrix) {
// Start typing your Java solution below
// DO NOT write main() function
if (null == matrix || matrix.length <= 0) {
return;
}
int roll = (matrix.length - 1) / 2;
for (int i = 0; i <= roll; i++) {
for (int k = i; k < matrix.length - i - 1; k++) {
int tmp = matrix[i][k];
matrix[i][k] = matrix[matrix.length - k - 1][i];
matrix[matrix.length - k - 1][i] = matrix[matrix.length
- i - 1][matrix.length - k - 1];
matrix[matrix.length - i - 1][matrix.length - k - 1] = matrix[k][matrix.length
- i - 1];
matrix[k][matrix.length - i - 1] = tmp;
}
}
}
Jerky Lu wechat
欢迎加入微信公众号